﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Yoozoo.Mars.Got.SpriteAnim
{
    public class SpriteResourceManager
    {
        private const string CONTENT_ROOT_STR = "FrameAnimation/";
        private const int INITIAL_CACHE_SIZE = 4;

        private Dictionary<string, Sprite[]> AltasBase;
        private Dictionary<string, uint> RLUDictionary;
        private List<string> NotUseList;
        private BaseSpriteCache spriteCache;
        //private 

        public void Dispose()
        {
            AltasBase.Clear();
            AltasBase = null;
            spriteCache = null;
        }
        public SpriteResourceManager(BaseSpriteCache spriteCache)
        {
            this.spriteCache = spriteCache;
            AltasBase = new Dictionary<string, Sprite[]>();
            RLUDictionary = new Dictionary<string, uint>();
            NotUseList = new List<String>();
        }
        
        public void SetSpritesUseTag(string altas, uint count)
        {
            RLUDictionary[altas] = RLUDictionary[altas] + count;
        }
        public void SetSpritesNotUseTag(string altas)
        {
            NotUseList.Add(altas);
            if (AltasBase.Count > INITIAL_CACHE_SIZE)
            {
                RemoveUnloadAltas();
            }
        }

        private void UnloadAltas(string altas)
        {
            string altasPath = CONTENT_ROOT_STR + altas;
            NotUseList.Remove(altasPath);
            AltasBase.Remove(altas);
            RLUDictionary.Remove(altas);
        }

        private void OnSpritesLoadFinished(Sprite[] spriteList, string altas)
        {
            if (spriteList == null)
            {
                throw new NullReferenceException("Cannot find altas!");
            }

            AltasBase.Add(altas, spriteList);
            RLUDictionary.Add(altas, 0);
        }
        public void RemoveUnloadAltas()
        {
            if (NotUseList.Count == 0)
            {
                return;
            }
            uint useTime = 0;
            uint getUseTime = 0;
            int indexNeedRemove = 0;
            for (int index = 0; index < NotUseList.Count; index++)
            {
                getUseTime = RLUDictionary[NotUseList[index]];
                if (getUseTime > useTime)
                {
                    useTime = getUseTime;
                    indexNeedRemove = index;
                }
            }
            UnloadAltas(NotUseList[indexNeedRemove]);
        }
        public SpriteAnimationX GetSprites(string altasName)
        {
            //TODO: 这个RLU有啥用呢》？？
            /*if (!AltasBase.ContainsKey(altasName))
            {
                if (AltasBase.Count >= INITIAL_CACHE_SIZE)
                {
                    RemoveUnloadAltas();
                }
                LoadAltas(altasName);
            }
            NotUseList.Remove(altasName);
            return AltasBase[altasName];*/
            RLUDictionary[altasName] = 0;
            SpriteAnimationX sprites;
            spriteCache.getSprites(altasName, out sprites);
            return sprites;
        }

        public void handleSprite(string altasName)
        {
            
        }
    }
}
